using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class KeHoachDaoTaoRepository
	{
		public static List<KeHoachDaoTao> SelectAll()
		{
			try
			{
				return DataContext.Instance.KeHoachDaoTao.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<KeHoachDaoTao>();
		}

		public static KeHoachDaoTao SelectByID(int makhdt)
		{
			try
			{
				return DataContext.Instance.KeHoachDaoTao.FirstOrDefault(item => item.MaKHDT == makhdt );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(KeHoachDaoTao obj)
		{
			try
			{
				DataContext.Instance.KeHoachDaoTao.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int makhdt)
		{
			try
			{
				var delitem = DataContext.Instance.KeHoachDaoTao.FirstOrDefault(item => item.MaKHDT == makhdt );
				DataContext.Instance.KeHoachDaoTao.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<KeHoachDaoTao> RetrieveByID(int makhdt)
		{
			try
			{
				return (from item in DataContext.Instance.KeHoachDaoTao where  item.MaKHDT == makhdt  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<KeHoachDaoTao>();
		}

		public static List<KeHoachDaoTao> SelectByMaCN(int macn)
		{
			try
			{
			var lstItem = (from item in DataContext.Instance.KeHoachDaoTao where item.MaCN == macn select item).ToList();
			return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<KeHoachDaoTao>();
		}

	}

}
